package com.traffic.between.adaptor;

import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.BetweenProperties;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.LiuliangServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import java.util.Date;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

@ChooseProvider(channel = "liuliang")
public class LiuliangAdaptor implements IAdaptor{
	private static Logger log = Logger.getLogger(LiuliangAdaptor.class);
	private static String HOST_CALLBACK_URL = BetweenProperties.CALLBACK_URL;//回调地址
	
	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		
		String clientOrderNo = orderBean.getIds();
		String productCode = orderBean.getProductCode();
		String phone = orderBean.getPhoneNo();
		String notifyUrl = HOST_CALLBACK_URL;
		log.info("流量服务订购接口请求参数 clientOrderNo:" + clientOrderNo+",productCode:"+productCode+",phone:"+phone+",notifyUrl:"+notifyUrl);
		try {
			String result = LiuliangServiceImpl.service.order(clientOrderNo, productCode, phone, notifyUrl);

			JSONObject json = JSONObject.fromObject(result);
			String code = (String) json.get("resultCode");
			if ("1000".equals(code)) {
			
				orderBean.setStatus("11");
				orderBean.setMessage("订单已受理");
				orderBean.setErrorMsg("处理中");
				if (json.containsKey("orderNo"))
					orderBean.setSuppOrderNo(json.getString("orderNo"));
				else {
					
					orderBean.setSuppOrderNo("");
				}
				
			} else if ("2000".equals(code)) {
				
				orderBean.setStatus("7");
				orderBean.setMessage("订购成功");
				orderBean.setErrorMsg("订购成功");
				if (json.containsKey("orderNo"))
					
					orderBean.setSuppOrderNo(json.getString("orderNo"));
				else {
					
					orderBean.setSuppOrderNo("");
				}
			} else if ("4000".equals(code)) {
				
				orderBean.setMessage("订购失败");
				if (json.containsKey("errMsg"))
					
					orderBean.setErrorMsg(json.getString("errMsg"));
				else {
					orderBean.setErrorMsg("订购失败");
				}
				orderBean.setStatus("8");
				if (json.containsKey("orderNo"))
					
					orderBean.setSuppOrderNo(json.getString("orderNo"));
				else {
					orderBean.setSuppOrderNo("");
				}
				
			} else if ("11111".equals(code)) {
				
				orderBean.setErrorMsg(json.getString("msg"));
				orderBean.setStatus("8");
			} else {
				
				orderBean.setMessage("订购中");
				if (json.containsKey("errMsg"))
				
					orderBean.setErrorMsg("返回resultCode值为[" + code + "], errMsg描述为[" + json.getString("errMsg") + "],系统默认当订购中处理.");
				else {
					
					orderBean.setErrorMsg("返回resultCode值为[" + code + "],系统默认当订购中处理.");
				}
				orderBean.setStatus("11");
				if (json.containsKey("orderNo"))
					
					orderBean.setSuppOrderNo(json.getString("orderNo"));
				else {
					
					orderBean.setSuppOrderNo("");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();

			orderBean.setErrorMsg("订购失败");
			orderBean.setStatus("8");
			orderBean.setMessage(e.getMessage());
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
}